package com.liuxvru.project.user.controller;

import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.core.page.TableDataInfo;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.common.utils.poi.ExcelUtil;
import com.liuxvru.project.domain.Release;
import com.liuxvru.project.service.IReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 发布Controller
 *
 * @author liuxvru
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/user/project/release")
public class UserReleaseController extends BaseController {
    @Autowired
    private IReleaseService releaseService;

    /**
     * 查询发布列表
     */
    @GetMapping("/list/{projectId}")
    public TableDataInfo list(@PathVariable("projectId") Long projectId, Release release) {
        startPage();
        release.setProjectId(projectId);
        List<Release> list = releaseService.selectReleaseList(release);
        return getDataTable(list);
    }
    /**
     * 查询发布列表
     */
    @GetMapping("/listAll/{projectId}")
    public AjaxResult listAll(@PathVariable("projectId") Long projectId, Release release) {
        release.setProjectId(projectId);
        List<Release> list = releaseService.selectReleaseList(release);
        return success(list);
    }

    /**
     * 导出发布列表
     */
    @Log(title = "发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Release release) {
        List<Release> list = releaseService.selectReleaseList(release);
        ExcelUtil<Release> util = new ExcelUtil<Release>(Release.class);
        util.exportExcel(response, list, "发布数据");
    }

    /**
     * 获取发布详细信息
     */
    @GetMapping(value = "/{releaseId}")
    public AjaxResult getInfo(@PathVariable("releaseId") Long releaseId) {
        return success(releaseService.getById(releaseId));
    }

    /**
     * 新增发布
     */
    @Log(title = "发布", businessType = BusinessType.INSERT)
    @PostMapping("{projectId}")
    public AjaxResult add(@PathVariable("projectId") Long projectId, @RequestBody Release release) {
        return toAjax(releaseService.add(projectId,release));
    }

    /**
     * 修改发布
     */
    @Log(title = "发布", businessType = BusinessType.UPDATE)
    @PutMapping("{projectId}")
    public AjaxResult edit(@PathVariable("projectId") Long projectId,@RequestBody Release release) {
        return toAjax(releaseService.edit(projectId,release));
    }

    /**
     * 删除发布
     */
    @Log(title = "发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{releaseIds}")
    public AjaxResult remove(@PathVariable Long[] releaseIds) {
        return toAjax(releaseService.removeById(releaseIds));
    }
    /**
     * 将迭代添加到发布中
     */
    @Log(title = "发布", businessType = BusinessType.INSERT)
    @PostMapping("/addSprint/{releaseIds}/{sprintId}")
    public AjaxResult addSprint(@PathVariable("releaseIds") Long releaseIds,@PathVariable("sprintId") Long sprintId) {
        return toAjax(releaseService.addSprint(releaseIds,sprintId));
    }
    /**
     * 将迭代移除发布
     */
    @Log(title = "发布", businessType = BusinessType.INSERT)
    @PostMapping("/removeSprint/{releaseIds}/{sprintId}")
    public AjaxResult removeSprint(@PathVariable("releaseIds") Long releaseIds,@PathVariable("sprintId") Long sprintId) {
        return toAjax(releaseService.removeSprint(releaseIds,sprintId));
    }


}
